package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcServiceItemMapper;
import com.csun.cmny.provider.dao.OmcServiceMapper;
import com.csun.cmny.provider.dto.ServiceQueryDto;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcService;
import com.csun.cmny.provider.model.domain.OmcServiceItem;
import com.csun.cmny.provider.model.vo.GroupVo;
import com.csun.cmny.provider.service.OmcOrderService;
import com.csun.cmny.provider.service.OmcServiceService;
import com.csun.cmny.provider.service.UacRpcService;
import com.csun.cmny.provider.vo.ServiceVo;
import com.csun.cmny.provider.vo.VolunteerVo;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xcr
 * @Date: 2019-05-25
 * @Description: the class omc ServiceItem controller
 */
@Slf4j
@Service
@Transactional
public class OmcServiceServiceImpl extends BaseService<OmcService> implements OmcServiceService {

    @Resource
    OmcServiceMapper omcServiceMapper;

    @Resource
    OmcServiceItemMapper omcServiceItemMapper;

    @Resource
    UacRpcService uacRpcService;
    @Resource
    OmcOrderService omcOrderService;
    @Resource
    OmcServiceService omcServiceService;

    /*
     * 分页查询服务
     * */
    @Override
    public PageInfo queryServiceListWithPage(ServiceQueryDto serviceQueryDto) {
        PageHelper.startPage(serviceQueryDto.getPageNum(), serviceQueryDto.getPageSize());
        List<ServiceVo> serviceVoList = omcServiceMapper.queryServiceListWithPage(serviceQueryDto);

        return new PageInfo<>(serviceVoList);
    }

    /*
     * 删除服务
     * */
    @Override
    public Integer deleteServiceAndItem(Long serviceId) {
        //删除服务项
        OmcServiceItem omcServiceItem = new OmcServiceItem();
        omcServiceItem.setServiceId(serviceId);
        int retDeleteItem = omcServiceItemMapper.delete(omcServiceItem);
        //删除服务
        int retDeleteService = omcServiceMapper.deleteByPrimaryKey(serviceId);
        if (retDeleteItem >= 0 && retDeleteService >= 0) {
            return 1;
        } else {
            return 0;
        }
    }

    /*
     * 添加修改服务
     * */
    @Override
    public Integer saveService(OmcService omcService, LoginAuthDto loginAuthDto) {

        omcService.setUpdateInfo(loginAuthDto);

        //同名校验
        checkName(omcService,loginAuthDto);

        if(omcService.isNew()){
            return omcServiceMapper.insertSelective(omcService);
        }else{
            return omcServiceMapper.updateByPrimaryKeySelective(omcService);
        }
    }
    //查询该平台下服务项目
    @Override
    public List<String> selectSeriviceItem(Long groupId) {

        /**
         * @date    2021-11-19
         * @author  chisj
         * @desc    获取子机构id集合
         */
        List<GroupVo> groupVoList = uacRpcService.queryGroupListByGroupId(groupId);
        if (PublicUtil.isEmpty(groupVoList)) {
            return Lists.newArrayList();
        }

        List<Long> groupIdList = Lists.newArrayList();
        for (GroupVo gv : groupVoList) {
            groupIdList.add(gv.getId());
        }
        return omcServiceMapper.selectSeriviceItem(groupIdList);
    }
    //查询服务项目占比
    @Override
    public Map<String, Number> selectServicePercent(Long groupId,List<String> services) {
        /**
         * @date    2021-11-19
         * @author  chisj
         * @desc    获取子机构id集合
         */
        List<GroupVo> groupVoList = uacRpcService.queryGroupListByGroupId(groupId);
        if (PublicUtil.isEmpty(groupVoList)) {
            return new HashMap<String, Number>();
        }

        List<Long> groupIdList = Lists.newArrayList();
        for (GroupVo gv : groupVoList) {
            groupIdList.add(gv.getId());
        }
        return omcServiceMapper.selectServicePercent(groupIdList,services);
    }
    //查询服务项目占比
    @Override
    public List<Map<String, Number>> selectServiceByNurseHome(Long groupId, List<String> services) {
        /**
         * @date    2021-11-19
         * @author  chisj
         * @desc    获取子机构id集合
         */
        List<GroupVo> groupVoList = uacRpcService.queryGroupListByGroupId(groupId);
        if (PublicUtil.isEmpty(groupVoList)) {
            return Lists.newArrayList();
        }

        List<Long> groupIdList = Lists.newArrayList();
        for (GroupVo gv : groupVoList) {
            groupIdList.add(gv.getId());
        }
        return omcServiceMapper.selectServiceByNurseHome(groupIdList,services);
    }
    /*
     * 大数据平台社区志愿者页面志愿者服务时长
     * */
    @Override
    public List<VolunteerVo> selectServiceTimeByNurseHome(VolunteerVo vo) {
        /**
         * @date    2021-11-19
         * @author  chisj
         * @desc    获取子机构id集合
         */
        List<GroupVo> groupVoList = uacRpcService.queryGroupListByGroupId(vo.getGroupId().longValue());
        if (PublicUtil.isEmpty(groupVoList)) {
            return Lists.newArrayList();
        }

        List<Long> groupIdList = Lists.newArrayList();
        for (GroupVo gv : groupVoList) {
            groupIdList.add(gv.getId());
        }
        List<VolunteerVo> volunteerVos = omcServiceMapper.selectServiceTimeByNurseHome(vo.getYear(), groupIdList);
        for (VolunteerVo volunteerVo:volunteerVos) {
            for (GroupVo g:groupVoList) {
                if(g.getId().equals(volunteerVo.getGroupId().longValue())){
                    volunteerVo.setGroupName(g.getGroupName());
                }
            }
        }
        return volunteerVos;
    }

    @Override
    public VolunteerVo selectByVolunteer(Long groupId) {
        /**
         * @date    2021-11-19
         * @author  chisj
         * @desc    获取子机构id集合
         */
        List<GroupVo> groupVoList = uacRpcService.queryGroupListByGroupId(groupId);
        if (PublicUtil.isEmpty(groupVoList)) {
            return new VolunteerVo();
        }

        List<Long> groupIdList = Lists.newArrayList();
        for (GroupVo gv : groupVoList) {
            groupIdList.add(gv.getId());
        }
        //查询志愿者人数
        Integer count = uacRpcService.selectVolunteerCount(groupId);
        //查询服务时长
        Integer serviceTime = omcOrderService.selectServiceTime(groupId);
        //查询该平台下服务项目
        List<String> services = omcServiceService.selectSeriviceItem(groupId);
        //查询服务项目占比
        Map<String, Number> volunteerService = null;
        if(services.size() > 0){
            volunteerService = omcServiceService.selectServicePercent(groupId,services);
        }
        VolunteerVo volunteerVo = new VolunteerVo();
        volunteerVo.setVolunteers(count);
        volunteerVo.setServiceTime(serviceTime);
        volunteerVo.setVolunteerService(volunteerService);
        return volunteerVo;
    }

    /*
     * 校验服务名称是否相同
     */
    public void checkName(OmcService omcService,LoginAuthDto loginAuthDto){
        Example example = new Example(OmcService.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("serviceName",omcService.getServiceName());
        if(omcService.getId() != null){
            criteria.andNotEqualTo("id",omcService.getId());
        }
        criteria.andEqualTo("groupId",loginAuthDto.getGroupId());
        int result = selectCountByExample(example);
        if(result > 0){
            throw new OmcBizException(ErrorCodeEnum.OMC10033003);
        }
    }
}